package com.m3958.firstgwt.client.window;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.client.datasource.GroupDataSource;
import com.m3958.firstgwt.client.types.MessagePurpose;
import com.m3958.firstgwt.shared.type.CommonField;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

@Singleton
public class FindGroupWindow extends Window{


	private ListGrid groupGrid;
	private DynamicForm findGroupForm;
	
	private GroupDataSource gds;
	
	@Inject
	private SendMessageWindow iw;
	
	@Inject
	public FindGroupWindow(GroupDataSource gds){
		this.gds = gds;
		setWidth(600);
		setHeight(450);
		setIsModal(false);
		setTitle("查找组");
		centerInPage();
		init();
	}
	

	private void init(){
		VLayout layout = new VLayout(5);
		layout.addMember(initSharedUserGrid());
		layout.addMember(initShareForm());
		layout.addMember(initBt());
		addItem(layout);
	}

	private Canvas initBt() {
		IButton ib = new IButton("查找");
		ib.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
					if(findGroupForm.getValue("gid") == null){
						groupGrid.filterData();
					}else{
						Criteria c;
						if(findGroupForm.getValue("byId") == null){
							c = new Criteria(CommonField.NAME.getEname(), findGroupForm.getValueAsString("gid"));
						}else{
							c = new Criteria(CommonField.ID.getEname(), findGroupForm.getValueAsString("gid"));
						}
						groupGrid.filterData(c);
					}
			}
		});
		return ib;
	}

	private Canvas initShareForm() {
		findGroupForm = new DynamicForm();
		findGroupForm.setNumCols(4);
		TextItem groupNameItem = new TextItem("gid","組id或名称");
		CheckboxItem byId = new CheckboxItem("byId", "组ID查找");
		findGroupForm.setFields(groupNameItem,byId);
		return findGroupForm;
	}
    
	private Canvas initSharedUserGrid() {
		groupGrid = new ListGrid();
		groupGrid.setDataSource(gds);
		groupGrid.setAutoFetchData(false);
		ListGridField idField = new ListGridField("id");
		ListGridField nameField = new ListGridField(CommonField.NAME.getEname(),"组名称");
        ListGridField requestField = new ListGridField("requestField","申请加入");
        requestField.setCellFormatter(new CellFormatter() {
			@Override
			public String format(Object value, ListGridRecord record, int rowNum,
					int colNum) {
				return "<span style=\"cursor:pointer;\">申请加入</span>";
			}
		});
        
        requestField.addRecordClickHandler(new RecordClickHandler() {
			@Override
			public void onRecordClick(RecordClickEvent event) {
				Record r = event.getRecord();
				String gn = r.getAttributeAsString(CommonField.NAME.getEname());
				iw.show("发送申请", "群:" + gn + "的管理者", "申请加入群", "", MessagePurpose.REQUEST_JOIN_GROUP, r.getAttributeAsString(CommonField.ID.getEname()));
			}
		});
		groupGrid.setFields(nameField,idField,requestField);
		return groupGrid;
	}
}
